package com.example.demo.gencode;

import com.alibaba.druid.DbType;
import com.alibaba.druid.pool.DruidDataSource;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.core.keygen.KeyGenerators;

public class Codegen {

    public static void main(String[] args) {
        //配置数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
        dataSource.setDbType(DbType.postgresql);
        dataSource.setUsername("postgres");
        dataSource.setPassword("123456");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        //GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        //globalConfig.setSourceDir("");
        globalConfig.setBasePackage("com.example.demo");

        //设置表前缀和只生成哪些表
        globalConfig.setTablePrefix("t_");
        globalConfig.setGenerateTable("t_user");
        globalConfig.setEntityOverwriteEnable(true);

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
        globalConfig.setEntityJdkVersion(17);
        globalConfig.setVersionColumn("version");
        globalConfig.setLogicDeleteColumn("deleted");
        globalConfig.setEntityWithSwagger(true);


        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        //设置生成 service
        globalConfig.setServiceGenerateEnable(true);
        //设置生成 serviceImpl
        globalConfig.setServiceImplGenerateEnable(true);
        //设置生成 controller
        globalConfig.setControllerGenerateEnable(true);
        //设置生成 mapperXml
        globalConfig.setMapperXmlGenerateEnable(true);

        //可以单独配置某个列
        ColumnConfig inserColumnConfig = new ColumnConfig();
        inserColumnConfig.setColumnName("create_time");
        inserColumnConfig.setOnInsertValue("now()");
        ColumnConfig updateColumnConfig = new ColumnConfig();
        updateColumnConfig.setColumnName("update_time");
        updateColumnConfig.setOnUpdateValue("now()");

        ColumnConfig idColumnConfig = new ColumnConfig();
        idColumnConfig.setColumnName("id");
        idColumnConfig.setPrimaryKey(true).setKeyType(KeyType.Generator).setKeyValue(KeyGenerators.snowFlakeId);

        globalConfig.setColumnConfig(inserColumnConfig);
        globalConfig.setColumnConfig(updateColumnConfig);
        globalConfig.setColumnConfig(idColumnConfig);


        return globalConfig;
    }

    public static GlobalConfig createGlobalConfigUseStyle2() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.test");

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix("tb_")
                .setGenerateTable("tb_account", "tb_account_session");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setJdkVersion(17);

        //设置生成 mapper
        globalConfig.enableMapper();

        //可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.getStrategyConfig()
                .setColumnConfig("tb_account", columnConfig);

        return globalConfig;
    }
}
